import os
import base64
import logging
import botpy
from DDbot.botpy.client import (
    C2CMessage,
    GroupMessage,
    Event,
)


appId = "102115625"  # 小萌机器人 企业版-未开通私聊
secret = "JFB73zwtqnkhecaYWUSQPONMLKJIIIII"

appId2 = "102075443"  # 小熊机器人 personal 版
secret2 = "TdozALWhs4GSeq2ERer4HUhu8Mao2GUj"

appId3 = "102669223"  # 小雨机器人 企业版
secret3 = "MsOuQwT0X4b8fDlJrPxV4dClKtT3dDnN"


from Panda.Reply import *
from Panda.Core.Log import logger

logger.setLevel(logging.DEBUG)


class MyClient(botpy.Client):
    async def on_ready(self):
        logger.info(f"robot 「{self.robot['username']}」 on_ready!")

    async def on_group_at_message_create(self, grp_msg: GroupMessage):
        """群聊消息"""
        logger.info(f"收到群消息 {grp_msg}")
        xinxi = check_msg(grp_msg.content, grp_msg, True, True)
        if not xinxi:
            return
        elif os.path.exists(xinxi):
            upload = await grp_msg.client.upload_grp_base64file(
                group_openid=grp_msg.group_openid,  # type: ignore
                file_type=1,  # 文件类型要对应上，具体支持的类型见方法说明
                file_data=base64.b64encode(open(xinxi, "rb").read()).decode(),
            )
            await grp_msg.reply(msg_type=7, media=upload)
        else:
            await grp_msg.reply(content=xinxi)

    async def on_c2c_message_create(self, c2c_msg: C2CMessage):
        """私聊消息"""
        logger.info(f"收到私聊消息 {c2c_msg}")
        xinxi = check_msg(c2c_msg.content, c2c_msg, True, True)
        logger.debug(f"{xinxi}")
        if not xinxi:
            return
            await c2c_msg.reply(msg_type=0, content="无返回结果")
        elif os.path.exists(xinxi):
            upload = await c2c_msg.client.upload_c2c_base64file(
                openid=c2c_msg.author.user_openid,
                file_type=1,  # 文件类型要对应上，具体支持的类型见方法说明
                file_data=base64.b64encode(open(xinxi, "rb").read()).decode(),
            )
            await c2c_msg.reply(msg_type=7, media=upload)
        else:
            await c2c_msg.reply(msg_type=0, content=xinxi)

    async def on_friend_add(self, c2c_event: Event):
        await c2c_event.reply(
            msg_type=0,
            content="欢迎使用小雨机器人！可以使用左下角[指令查询]快捷查询用法！",
        )


if __name__ == "__main__":

    client = MyClient(intents=1 << 25)
    client.run(appid=appId2, secret=secret2)
